import R from "@/common/type/response";
import { NextApiResponse } from "next";
import { SysMenuService } from "@/common/service/sys_menu";
import { AuthenticatedRequest, withAuth } from "@/common/middleware/auth";

export async function handler(req: AuthenticatedRequest, res: NextApiResponse) {
  const { id } = req.query;
  if (!id) {
    R.error(res, "缺少菜单ID");
    return;
  }
  try {
    if (req.method === "GET") {
      const data = await SysMenuService.getById(Number(id));
      R.success(res, data);
    } else if (req.method === "PUT" || req.method === "PATCH") {
      await SysMenuService.update(Number(id), req.body);
      R.success(res);
    } else if (req.method === "DELETE") {
      await SysMenuService.delete(Number(id));
      R.success(res);
    } else {
      res.status(405).json({ code: 405, msg: "Method Not Allowed", data: null });
    }
  } catch (e: any) {
    R.error(res, e.message);
  }
}

export default withAuth(handler); 